package br.com.klis.batendoumabola.client;

import br.com.klis.batendoumabola.client.event.AddPeladaEvent;
import br.com.klis.batendoumabola.client.event.AddPeladaEventHandler;
import br.com.klis.batendoumabola.client.event.EditPeladaCancelledEvent;
import br.com.klis.batendoumabola.client.event.EditPeladaCancelledEventHandler;
import br.com.klis.batendoumabola.client.event.EditPeladaEvent;
import br.com.klis.batendoumabola.client.event.EditPeladaEventHandler;
import br.com.klis.batendoumabola.client.event.NovoBateBolaEvent;
import br.com.klis.batendoumabola.client.event.NovoBateBolaEventHandler;
import br.com.klis.batendoumabola.client.event.PeladaUpdatedEvent;
import br.com.klis.batendoumabola.client.event.PeladaUpdatedEventHandler;
import br.com.klis.batendoumabola.client.event.SignInEvent;
import br.com.klis.batendoumabola.client.event.SignInEventHandler;
import br.com.klis.batendoumabola.client.event.UploadEvent;
import br.com.klis.batendoumabola.client.event.UploadEventHandler;
import br.com.klis.batendoumabola.client.presenter.PeladasPresenter;
import br.com.klis.batendoumabola.client.presenter.Presenter;
import br.com.klis.batendoumabola.client.presenter.UploadPresenter;
import br.com.klis.batendoumabola.client.service.PeladaServiceAsync;
import br.com.klis.batendoumabola.client.service.UserImageService;
import br.com.klis.batendoumabola.client.service.UserImageServiceAsync;
import br.com.klis.batendoumabola.client.view.PeladasView;
import br.com.klis.batendoumabola.client.view.UploadView;
import br.com.klis.batendoumabola.shared.LoginInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.ui.HasWidgets;

public class AppController implements Presenter, ValueChangeHandler<String> {
  private final HandlerManager eventBus;
  private final PeladaServiceAsync rpcService;
  private final UserImageServiceAsync userImageService = GWT.create(UserImageService.class);
  private HasWidgets container;
  private LoginInfo loginInfo;
  
  public AppController(PeladaServiceAsync rpcService, HandlerManager eventBus, LoginInfo loginInfo) {
    this.eventBus = eventBus;
    this.rpcService = rpcService;
    this.loginInfo = loginInfo;
    bind();
  }
  
  private void bind() {
	  GWT.log("init app controller bind...");
    History.addValueChangeHandler(this);

    eventBus.addHandler(SignInEvent.TYPE, new SignInEventHandler() {
		public void onSignIn(SignInEvent event) {
			GWT.log("eventBus manipulando eventoSignIn...");
			reinit();
		}
	});
    
    eventBus.addHandler(NovoBateBolaEvent.TYPE,
    		new NovoBateBolaEventHandler() {
            public void onNovoBateBola(NovoBateBolaEvent event) {
                doNovoBateBola();
              }
    }); 
    
    eventBus.addHandler(AddPeladaEvent.TYPE,
        new AddPeladaEventHandler() {
        public void onAddPelada(AddPeladaEvent event) {
            doAddNewPelada();
        }
    });  

    eventBus.addHandler(EditPeladaEvent.TYPE,
        new EditPeladaEventHandler() {
        public void onEditPelada(EditPeladaEvent event) {
        	doEditPelada();
        }
    });  

    eventBus.addHandler(EditPeladaCancelledEvent.TYPE,
        new EditPeladaCancelledEventHandler() {
          public void onEditPeladaCancelled(EditPeladaCancelledEvent event) {
            doEditPeladaCancelled();
          }
        });  

    eventBus.addHandler(PeladaUpdatedEvent.TYPE,
        new PeladaUpdatedEventHandler() {
          public void onPeladaUpdated(PeladaUpdatedEvent event) {
            doPeladaUpdated();
          }
        });  
    
    eventBus.addHandler(UploadEvent.TYPE,
            new UploadEventHandler() {
            public void onUpload(UploadEvent event) {
                doUpload();
            }
        }); 
  }
  
  private void reinit(){
	  GWT.log("executando metodo go do app controler...");
	  go(container);
  }
  
  private void doNovoBateBola(){
	  GWT.log("doNovoBateBola...");
	  History.newItem("add");
  }
  private void doAddNewPelada() {
    History.newItem("add");
  }
  
  private void doEditPelada() {
	  GWT.log("doEditBateBola...");
    History.newItem("edit", false);
    //Presenter presenter = new EditPeladaPresenter(rpcService, eventBus, new EditPeladaView(), id);
    //presenter.go(container);
  }
  
  private void doEditPeladaCancelled() {
    History.newItem("list");
  }
  
  private void doPeladaUpdated() {
    History.newItem("list");
  }
  
  private void doUpload() {
	  GWT.log("upload...");
    History.newItem("up", false);
    //userImageService = GWT.create(UserImageService.class);
    Presenter presenter = new UploadPresenter(userImageService, eventBus, new UploadView(userImageService, loginInfo));
    presenter.go(container);
  }
  
  public void go(final HasWidgets container) {
	GWT.log("init app controller go...");
    this.container = container;
    
    if ("".equals(History.getToken())) {
    	GWT.log("history token vazio...");
      History.newItem("list");
    }
    else {
      History.fireCurrentHistoryState();
    }
  }

  public void onValueChange(ValueChangeEvent<String> event) {
    String token = event.getValue();
    
    if (token != null) {
      Presenter presenter = null;

      if (token.equals("list") || token.equals("add") || token.equals("edit")) {
    	  GWT.log("app controller on value change list...");
    	  presenter = new PeladasPresenter(rpcService, eventBus, new PeladasView(loginInfo),loginInfo);
      }
      else if (token.equals("up")) {
        presenter = new UploadPresenter(userImageService, eventBus, new UploadView(userImageService, loginInfo));
    	//  presenter = new ContentPresenter(rpcService, eventBus, new ContentVerticalPanel(),"EDICAO");
      }
      else if (token.equals("edit")) {
        //presenter = new EditPeladaPresenter(rpcService, eventBus, new EditPeladaView());
      }
      
      if (presenter != null) {
        presenter.go(container);
      }
    }
  }
}
